package com.lagou.cl.pojo;

import com.lagou.cl.util.HttpProtocolUtil;
import com.lagou.cl.util.StaticResourceUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;


public class Response {


	private OutputStream outputStream;


	public Response() {
	}

	public Response(OutputStream outputStream) {
		this.outputStream = outputStream;
	}

	public OutputStream getOutputStream() {
		return outputStream;
	}

	public void setOutputStream(OutputStream outputStream) {
		this.outputStream = outputStream;
	}

	public void outHtml(String url) throws IOException {
		//根据url获取静态资源的绝对路径

		String resourcePath = StaticResourceUtil.getAbsolutePath(url.replace("/", ""));
		resourcePath = URLDecoder.decode(resourcePath, "UTF-8");
		System.out.println(resourcePath);
		//String resourcePath = "E:\\学习资料\\拉勾教育\\project_test\\tomcat\\miniCat\\target\\classes\\index.html";
		File file = new File(resourcePath);
		if (file.exists() && file.isFile()) {
			StaticResourceUtil.outStaticResouce(new FileInputStream(file), this.outputStream);
		} else {
			//404
			out(HttpProtocolUtil.get_404());
		}

	}

	public void out(String content) throws IOException {
		this.outputStream.write(content.getBytes(StandardCharsets.UTF_8));
	}

}
